/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  PersonInfoHuoDongActivity.java   
 * @Package com.jiyi.activity.zone   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-16 上午9:28:04   
 * @version V1.0     
 */
package com.jiyi.activity.zone;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.adapter.DividerItemDecoration;
import com.jiyi.adapter.recyclerview.CommonAdapter;
import com.jiyi.adapter.recyclerview.base.ViewHolder;
import com.jiyi.adapter.recyclerview.wrapper.EmptyWrapper;
import com.jiyi.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;
import com.jiyi.bean.zone.HuoDongDetailJson;
import com.jiyi.bean.zone.ZoneHuoDongBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.HuoDongPopInterface;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.HuoDongPopView;
import com.jiyi.widget.PopTextView;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.utils.DateUtils;
import com.swift.widget.TranslucentImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: PersonInfoHuoDongActivity
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-16 上午9:28:04
 * 
 */
public class PersonInfoHuoDongFragment extends Fragment implements HuoDongPopInterface{

	@BindView(R.id.id_stickynavlayout_innerscrollview)
	RecyclerView mListView;
	private List<ZoneHuoDongBean> mHuoDongDatas = new ArrayList<ZoneHuoDongBean>();
	EmptyWrapper adapter;
	public HuoDongPopView mHuoDongPopView;
	 ViewGroup container;
	LayoutInflater inflater;
	//用户id
	String userId;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_tab, container, false);
		ButterKnife.bind(this, view);
		this.inflater = inflater;
		this.container = container;
		getParams();
		setAdapter();
		mHuoDongPopView = new HuoDongPopView(getActivity(), this);
		return view;

	}
	
	/**   
	 * @author: HYM 
	 * @Title: setAdapter   
	 * @Description: TODO(这里用一句话描述这个方法的作用)    void 
	 * @date: 2017-7-26
	 */  
	private void setAdapter() {
		mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
		mListView.addItemDecoration(new DividerItemDecoration(getActivity(),
				DividerItemDecoration.VERTICAL_LIST, getResources()
						.getDrawable(R.drawable.drvice_10)));
		CommonAdapter mCommonAdapter = new CommonAdapter<ZoneHuoDongBean>(getActivity(),
				R.layout.item_zone_huodong, mHuoDongDatas) {
			@Override
			protected void convert(ViewHolder holder, final ZoneHuoDongBean t,
					int position) {
				holder.setText(R.id.tvTitle, t.getTheme());
				holder.setImage(R.id.ivHuoDongLogo, t.getCover());
				holder.setText(R.id.tvStartDate, DateUtils.fmtDateToStr(t.getStart_time(), DateUtils.YMD_FORMAT));
				holder.setText(R.id.tvEndDate, DateUtils.fmtDateToStr(t.getEnd_time(), DateUtils.YMD_FORMAT));
				holder.setText(R.id.tvAddress, t.getAddress());
				TranslucentImageView ivHuoDongLogo = holder.getView(R.id.ivHuoDongLogo);
				ivHuoDongLogo.setTextString(t.getAttendPer() + "人参与");
				switch (t.getFlag()) {
				case 1:
					holder.setText(R.id.tvJoin, "等待报名");
					holder.setBackgroundRes(R.id.tvJoin, R.drawable.round_huodong_state_small_blue);
					break;
				case 2:
					holder.setText(R.id.tvJoin, "申请参与");
					holder.setBackgroundRes(R.id.tvJoin, R.drawable.round_huodong_state_small_blue);
					break;
				case 3:
					holder.setText(R.id.tvJoin, "待开始");
					holder.setBackgroundRes(R.id.tvJoin, R.drawable.round_huodong_state_small_blue);
					break;
				case 4:
					holder.setText(R.id.tvJoin, "进行中");
					holder.setBackgroundRes(R.id.tvJoin, R.drawable.round_huodong_state);
					break;
				case 5:
					holder.setText(R.id.tvJoin, "活动结束");
					holder.setBackgroundRes(R.id.tvJoin, R.drawable.round_huodong_state_gray);
					break;
				case 6:
					holder.setText(R.id.tvJoin, "已取消");
					holder.setBackgroundRes(R.id.tvJoin, R.drawable.round_huodong_state_gray);
					break;
				default:
					break;
				}
				
				holder.setOnClickListener(R.id.tvJoin, new OnClickListener(){

					@Override
					public void onClick(View v) {
						getHuoDongDetail(t.getId());
					}
					
				});
			}
		};
		HeaderAndFooterWrapper headAdapter = new HeaderAndFooterWrapper(
				mCommonAdapter);
		View headView = inflater.inflate(R.layout.head_person_info_huodong,
				null, false);
		headView.setTag("head");
		final PopTextView ptvState = (PopTextView) headView
				.findViewById(R.id.tvType);
		final TextView tvTypeName = (TextView) headView
				.findViewById(R.id.tvTypeName);
		ArrayAdapter<String> stateAdapter;
		// 设置报修状态的适配器
		stateAdapter = new ArrayAdapter<String>(getActivity(),
				R.layout.item_huodong_join_state, getResources()
						.getStringArray(R.array.person_huodong_state));
		ptvState.setListAdapter(stateAdapter);
		ptvState.setOnItemClick(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String text = ((TextView) view).getText().toString();
				ptvState.doListItemClick("筛选活动", position + "");
				tvTypeName.setText(text);
				doFliter(userId,position+1);
			}
		});
		headAdapter.addHeaderView(headView);
		
		if (mHuoDongDatas.size() >0) {
			adapter = new EmptyWrapper(headAdapter);
		}
		else {
			adapter = new EmptyWrapper(mCommonAdapter);
		}
		
		View emptyView = inflater.inflate(R.layout.empty_view_text_and_image, container, false);
		ImageView ivEmptyTip = (ImageView) emptyView.findViewById(R.id.ivEmptyTip);
		TextView tvEmptyTip = (TextView) emptyView.findViewById(R.id.tvEmptyTip);
		ivEmptyTip.setImageResource(R.mipmap.tip_empty_huodong);
		tvEmptyTip.setText("TA 还没有参与任何活动哦 ~");
		adapter.setEmptyView(emptyView);
		
		mListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}

	/**   
	 * @author: HYM 
	 * @Title: getParams   
	 * @Description: TODO(这里用一句话描述这个方法的作用)    void 
	 * @date: 2017-7-26
	 */  
	private void getParams() {
		userId = getArguments().getString("id");
		String datas = (String) getArguments().get("datas");
		mHuoDongDatas = new Gson().fromJson(datas,
				new TypeToken<List<ZoneHuoDongBean>>() {
				}.getType());		
	}

	void getHuoDongDetail(String huoDongId)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", huoDongId);

		VolleyRequest.StringRequestPost(getActivity(), WebApi.ZONE_HUODONG_DETAIL, "",
				params, new StringInterface(getActivity()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								HuoDongDetailJson temp = new Gson().fromJson(
										result, HuoDongDetailJson.class);
								mHuoDongPopView.showPopWindow(temp.getData());
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}
	
	void getHuoDongType(int type)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("type", type+"");

		VolleyRequest.StringRequestPost(getActivity(), WebApi.ZONE_HUODONG_DETAIL, "",
				params, new StringInterface(getActivity()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								HuoDongDetailJson temp = new Gson().fromJson(
										result, HuoDongDetailJson.class);
								mHuoDongPopView.showPopWindow(temp.getData());
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}

	/**   
	 * <p>Title: OnSureListener</p>   
	 * <p>Description: </p>      
	 * @see HuoDongPopInterface#OnSureListener()
	 */ 
	@Override
	public void OnSureListener() {
		// TODO Auto-generated method stub
		
	}

	/**   
	 * <p>Title: OnCancleListener</p>   
	 * <p>Description: </p>      
	 * @see HuoDongPopInterface#OnCancleListener()
	 */ 
	@Override
	public void OnCancleListener() {
		// TODO Auto-generated method stub
		
	}

	/**   
	 * <p>Title: OnShowListener</p>   
	 * <p>Description: </p>      
	 * @see HuoDongPopInterface#OnShowListener()
	 */ 
	@Override
	public void OnShowListener() {
		// TODO Auto-generated method stub
		
	}

	/**   
	 * <p>Title: OnHideListener</p>   
	 * <p>Description: </p>      
	 * @see HuoDongPopInterface#OnHideListener()
	 */ 
	@Override
	public void OnHideListener() {
		// TODO Auto-generated method stub
		
	}
	
	void doFliter(String id,int position)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", id);
		params.put("fliter", position+"");
		
		VolleyRequest.StringRequestPost(getContext(), WebApi.ZONE_PERSON_HUODONG_FLITER, "", params,
				new StringInterface(getContext()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								JSONObject dataJson = new JSONObject(obj.getString("data"));
								String actListString = dataJson.getString("actList");
								List<ZoneHuoDongBean> temp = new Gson().fromJson(actListString, new TypeToken<List<ZoneHuoDongBean>>() {
				}.getType());
								if (null != temp) {
									mHuoDongDatas.clear();
									mHuoDongDatas.addAll(temp);
									adapter.notifyDataSetChanged();
								}
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
//							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
//						hideDialog(dialog);
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}
}
